package registnet.dao;

import java.util.List;
import java.util.ListIterator;
import java.util.Vector;

import org.apache.torque.TorqueException;
import org.apache.torque.util.Criteria;

import registnet.tokenizer.RegistTokenizer;
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class CollegePeer extends registnet.dao.BaseCollegePeer {
	String logUnitCode = new String();
	String user = new String();
	/**
	 * Default constructor
	 */
	public CollegePeer(){
	}
	
	
	/**
	 * Inserts a new college
	 * 
	 * @param collegeCode - The code assigned to a college
	 * @param collegeName - The name of the college
	 * @return	The autogenerated id of the college
	 */
	public Integer createNewCollege(String collegeCode, String collegeName){
		Integer id = null;
		Criteria criteria = new Criteria();
		criteria.add(CollegePeer.COLLEGECODE, collegeCode);
		criteria.and(CollegePeer.COLLEGENAME, collegeName);
		try{
			id = Integer.parseInt(CollegePeer.doInsert(criteria).toString());
			java.util.Calendar cal = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			
			CollegeLogPeer.createNewCollegeLog(1, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]),collegeCode, collegeName, this.user, this.logUnitCode);
		}catch (TorqueException e) {
			e.printStackTrace();
		}
		return id;
	}
	
	public Integer createRollbackCollege(String collegeCode, String collegeName){
		Integer id = null;
		Criteria criteria = new Criteria();
		criteria.add(CollegePeer.COLLEGECODE, collegeCode);
		criteria.and(CollegePeer.COLLEGENAME, collegeName);
		try{
			Integer.parseInt(CollegePeer.doInsert(criteria).toString());
			java.util.Calendar cal = Calendar.getInstance();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			
			id = CollegeLogPeer.createNewCollegeLog(1, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]),collegeCode, collegeName, this.user, this.logUnitCode);
		}catch (TorqueException e) {
			e.printStackTrace();
		}
		return id;
	}
	
	
	/**
	 * Gets the autogenerated id of the college
	 * 
	 * @param collegeCode - The code assigned to a college
	 * @return	The autogenerated id of the college
	 */
	public Integer getCollegeId(String collegeCode){
		Criteria criteria = new Criteria();
		criteria.add(CollegePeer.COLLEGECODE, collegeCode);
		List list;
		try{
			list = CollegePeer.doSelect(criteria);
			College college = (College)list.get(0);
			return college.getCollegeId();
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Gets the college with the given id
	 * 
	 * @param collegeId - The autogenerated id of the college
	 * @return	The college with the given id
	 */
	public College getCollege(Integer collegeId){
		Criteria criteria = new Criteria();
		criteria.add(CollegePeer.COLLEGEID, collegeId);
		List list;
		try{
			list = CollegePeer.doSelect(criteria);
			return (College)list.get(0);
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Gets the college with the given college code
	 * 
	 * @param collegeCode The college code assigned to a college
	 * @return	The college with the given college code
	 */
	public College getCollege(String collegeCode){
		Criteria criteria = new Criteria();
		criteria.add(CollegePeer.COLLEGECODE, collegeCode);
		try{
			College c = (College)CollegePeer.doSelect(criteria).get(0);
			return c;
		}catch (Exception e){
			return null;
		}
	}
	
	
	/**
	 * Updates the college with the given id
	 * 
	 * @param collegeId - The autogenerated id of the college
	 * @param collegeCode - The code assigned to a college
	 * @param collegeName - The name of the college
	 * @return	true if the update is successful, false otherwise
	 */
	public boolean updateCollege(Integer collegeId, String collegeCode, String collegeName){
		Criteria criteria = new Criteria();
		criteria.add(CollegePeer.COLLEGEID, collegeId);
		criteria.and(CollegePeer.COLLEGECODE, collegeCode);
		criteria.and(CollegePeer.COLLEGENAME, collegeName);
		try{
			Criteria c = new Criteria();
			c.add(CollegePeer.COLLEGEID, collegeId);
			College college = (College) CollegePeer.doSelect(c).get(0);
			Calendar cal = Calendar.getInstance();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			
			CollegeLogPeer.createNewCollegeLog(2, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), college.getCollegeCode(), college.getCollegeName(), this.user, this.logUnitCode);
			CollegePeer.doUpdate(criteria);
			CollegeLogPeer.createNewCollegeLog(4, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), collegeCode, collegeName, this.user, this.logUnitCode);
			return true;
		}catch (TorqueException e){
			e.printStackTrace();
			return false;
		}
	}
	
	public Integer updateRollbackCollege(Integer collegeId, String collegeCode, String collegeName){
		Integer id=null;
		Criteria criteria = new Criteria();
		criteria.add(CollegePeer.COLLEGEID, collegeId);
		criteria.and(CollegePeer.COLLEGECODE, collegeCode);
		criteria.and(CollegePeer.COLLEGENAME, collegeName);
		try{
			Criteria c = new Criteria();
			c.add(CollegePeer.COLLEGEID, collegeId);
			College college = (College) CollegePeer.doSelect(c).get(0);
			Calendar cal = Calendar.getInstance();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			
			
			CollegePeer.doUpdate(criteria);
			id = CollegeLogPeer.createNewCollegeLog(2, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), college.getCollegeCode(), college.getCollegeName(), this.user, this.logUnitCode);
			CollegeLogPeer.createNewCollegeLog(4, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), collegeCode, collegeName, this.user, this.logUnitCode);
			return id;
		}catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Deletes the college with the given id
	 * 
	 * @param collegeId - the autogenerated id
	 * @return	true if the delete is successful, false otherwise
	 */
	public boolean deleteCollege(Integer collegeId){
		Criteria criteria = new Criteria();
		criteria.add(CollegePeer.COLLEGEID, collegeId);
		try{
			College college = (College) CollegePeer.doSelect(criteria).get(0);
			CollegePeer.doDelete(criteria);Calendar cal = Calendar.getInstance();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			CollegeLogPeer.createNewCollegeLog(3, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), college.getCollegeCode(), college.getCollegeName(), this.user, this.logUnitCode);
			return true;
		}catch (TorqueException e){
			e.printStackTrace();
			return false;
		}
	}
	
	public Integer deleteRollbackCollege(Integer collegeId){
		Integer id=null;
		Criteria criteria = new Criteria();
		criteria.add(CollegePeer.COLLEGEID, collegeId);
		try{
			College college = (College) CollegePeer.doSelect(criteria).get(0);
			CollegePeer.doDelete(criteria);Calendar cal = Calendar.getInstance();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
			
			String dateFormat = sdf.format(cal.getTime());
			String[] date = dateFormat.split("-");
			
			String timeFormat = sdf1.format(cal.getTime());
			String[] time = timeFormat.split(":");
			id = CollegeLogPeer.createNewCollegeLog(3, Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]) , Integer.parseInt(time[0]), Integer.parseInt(time[1]), Integer.parseInt(time[2]), college.getCollegeCode(), college.getCollegeName(), this.user, this.logUnitCode);
			return id;
		}catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Gets all the colleges
	 * 
	 * @return	All the colleges
	 */
	public List<College> getAllColleges(){
		Criteria criteria = new Criteria();
		criteria.addAscendingOrderByColumn(COLLEGECODE);
		try{
			return CollegePeer.doSelect(criteria);
		}
		catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Gets all the colleges that match the keywords
	 * 
	 * @param collegeCode - The code assigned to a college
	 * @param collegeName - The name of the college
	 * @return	The colleges that match the keywords
	 */
	public List<College> searchCollege(String collegeCode, String collegeName){
		Criteria criteria = new Criteria();
		criteria.setIgnoreCase(true);
		criteria.addAscendingOrderByColumn(COLLEGECODE);
		
		Criteria.Criterion c1 = criteria.getNewCriterion(CollegePeer.COLLEGECODE, "%" + collegeCode + "%", Criteria.LIKE);
		Criteria.Criterion c2 = criteria.getNewCriterion(CollegePeer.COLLEGENAME, "%" + collegeName + "%", Criteria.LIKE);
		criteria.add(c1.and(c2));
		
		try{
			return CollegePeer.doSelect(criteria);
		}
		catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Imports colleges from a text file 
	 * 
	 * @param source - A line in the text file containing the info about a college
	 * @return	College codes of the colleges that were successfuly imported
	 */
	public List<String> importColleges(String source){
		List<String> token = RegistTokenizer.tokenize(source);
		List<String> collegeCodes = new Vector<String>();
		ListIterator<String> i = token.listIterator();
		while(i.hasNext()){
			String[] temp = i.next().split(",");
			if (temp.length != 2)
				return collegeCodes;
			try{
			String collegeCode = temp[0];
			String collegeName = temp[1];
			this.createNewCollege(collegeCode, collegeName);
			collegeCodes.add(collegeCode);
			} catch (Exception e) {/* write to log */}
		}
		
		return collegeCodes;
	}
	
	public void setUser(String user, String logUnitCode){
		this.user = user;
		this.logUnitCode = logUnitCode;
	}
}
